//
//  StyleViewController.m
//  图片故事
//
//  Created by 大帅逼 on 16/1/7.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "StyleViewController.h"
#import "CreateManagerViewController.h"
#import "Header.h"
#import "LocaMarkData.h"
#import "MBProgressHUD+PKX.h"

@interface StyleViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) NSMutableArray      *markArray;             //保存在本地的标签历史
@property(nonatomic,strong) LocaMarkData        *locaMarkData;          //标签本地数据存取
@property(nonatomic,strong) UITableView         *tableView;             //展示标签的表格视图
@property(nonatomic,strong) UIView              *headView;              //头部视图
@property(nonatomic,strong) UITextField         *markText;

@end

@implementation StyleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    
    // 初始化页面
    [self initView];
    
    // 初始化导航视图
    [self initNavigation];
}

#pragma mark -- 初始化数据
- (void) initData {

    _markArray = [[NSMutableArray alloc] init];
    
    //初始化本地标签读取
    _locaMarkData = [[LocaMarkData alloc] init];
    
    //从本地获取标签列表
    if ([_locaMarkData readMark] != nil) {
        [_markArray addObjectsFromArray:[_locaMarkData readMark]];
    }
   
    [_tableView reloadData];
}

/* - - - - - - - - - - - - - - -页面相关设置 - - - - - - - - - - - -  */

- (void) initView {
    
    [self.view setBackgroundColor:Bg_Color];
    
    self.title = @"填写标签";
    
    [self.view addSubview:self.tableView];
}

/* - - - - - - - - - - - - - - -设置导航控制器样式 - - - - - - - - - - - -  */

- (void) initNavigation {
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];
    
    // 设置导航控制器左右按钮
    UIBarButtonItem *backItme = [[UIBarButtonItem alloc] initWithTitle:@"返回 " style:UIBarButtonItemStylePlain target:self action:@selector(action_backItmeClickEvent)];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.leftBarButtonItem = backItme;
}

/* - - - - - - - - - - - - - - -点击事件相关设置 - - - - - - - - - - - -  */

- (void) action_backItmeClickEvent {
    
    [[CreateManagerViewController shardManger] showStyleViewController];
}

- (void) action_createItemClickEvent {
    
    if (_markText.text.length != 0) {
        
        [_markArray addObject:_markText.text];
        
        [_locaMarkData saveMark:_markArray];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setMark" object:_markText.text];
        
        [_markText resignFirstResponder];
        
        _markText.text = nil;
        
        [self.tableView reloadData];
        
        [[CreateManagerViewController shardManger] showStyleViewController];
    }else {
    
        [MBProgressHUD showError:@"标签不能为空"];
    }
}

#pragma mark --数据源方法
//设置章节数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _markArray.count;
}
//设置每行的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MY_TEXT"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MY_TEXT"];
    }
    
    NSString *searchText = [_markArray objectAtIndex:indexPath.row];
    
    [cell.textLabel setText:searchText];
    
    [cell.textLabel setTextColor:[UIColor grayColor]];
    
    [cell setBackgroundColor:Btn_Color];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return My_Height(20);
}

//tableview的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"setMark" object:[_markArray objectAtIndex:indexPath.row]];
    
    [[CreateManagerViewController shardManger] showStyleViewController];
}
#pragma mark --搜索框的代理方法

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    
    if (searchBar.text.length != 0) {
        
        [_markArray addObject:searchBar.text];
        
        [_locaMarkData saveMark:_markArray];
    }
}

#pragma mark --页面的显示与销毁
- (void)viewWillAppear:(BOOL)animated{
    
    [self.navigationController setNavigationBarHidden:NO];
    
}

#pragma mark -- 懒加载
- (UITableView *)tableView {

    if (!_tableView) {
        
        //初始化表格视图
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, CGRectGetMaxX(self.view.bounds), CGRectGetMaxY(self.view.bounds)) style:UITableViewStyleGrouped];
        
        [_tableView setDataSource:self];
        
        [_tableView setDelegate:self];
        
        [_tableView setBackgroundColor:Bg_Color];
        
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        
        [_tableView setSeparatorColor:[UIColor grayColor]];
        
        _tableView.tableFooterView = footerView;
        
        _tableView.tableHeaderView = self.headView;
    }
    return _tableView;
}

- (UIView *)headView {

    if (!_headView) {
        
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, My_Height(50))];
        
        _markText = [[UITextField alloc] initWithFrame:CGRectMake(My_Height(10), My_Height(5), SCREEN_W - 80, My_Height(40))];
        
        [_markText setLeftViewMode:UITextFieldViewModeAlways];
        
        [_markText setBackgroundColor:Btn_Color];
        
        [_markText.layer setCornerRadius:10];
        
        [_markText setClearButtonMode:UITextFieldViewModeAlways];
    
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, My_Width(20), My_Width(40))];
        
        [_markText setLeftView:leftView];
        
        [_markText setPlaceholder:@"输入标签"];
        
        [_markText.layer setBorderColor:[UIColor grayColor].CGColor];
        
        [_markText setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
        
        [_headView addSubview:_markText];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        
        [sureBtn setFrame:CGRectMake(SCREEN_W - My_Height(60), My_Height(5), My_Height(40), My_Height(40))];
        
        [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        
        [sureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        [sureBtn addTarget:self action:@selector(action_createItemClickEvent) forControlEvents:UIControlEventTouchUpInside];
        
        [_headView addSubview:sureBtn];
    }
    
    return _headView;
}

@end
